package br.brpv.silvestrin.conferencia.activity;

import java.util.ArrayList;

import br.brpv.silvestrin.conferencia.R;
import br.brpv.silvestrin.conferencia.dialogs.AddInvoiceDialog;
import br.brpv.silvestrin.conferencia.dialogs.AddLoteDialog;
import br.brpv.silvestrin.conferencia.dialogs.AddInvoiceDialog.AddInvoiceDialogListener;
import br.brpv.silvestrin.conferencia.dialogs.AddLoteDialog.AddLoteDialogListener;
import br.brpv.silvestrin.conferencia.dialogs.LoteOriginEditDialog.LoteOriginEditDialogListener;
import br.brpv.silvestrin.conferencia.dialogs.LoteOriginEditDialog;
import br.brpv.silvestrin.conferencia.models.Company;
import br.brpv.silvestrin.conferencia.models.LoteOrigin;
import br.brpv.silvestrin.conferencia.models.User;
import br.brpv.silvestrin.conferencia.models.adapters.LoteOriginAdapter;
import br.brpv.silvestrin.conferencia.webservice.CompanyWebService;
import br.brpv.silvestrin.conferencia.webservice.CompanyWebService.CompanyWebServiceListener;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Spinner;
import android.support.v4.app.NavUtils;

public class OriginActivity extends Activity implements CompanyWebServiceListener, OnClickListener, AddLoteDialogListener, AddInvoiceDialogListener, OnItemClickListener, LoteOriginEditDialogListener {
	public static final String LOGGED_USER_EXTRA = "logged_user_extra";
	
	private User loggedUser;
	CompanyWebService companyWebService;
	
	private Spinner companySpinner;
	private ListView lotesListView;
	private Button addLote;
	private Button addLoteFromInvoice;
	private Button destinationButton;
	
	private LoteOriginAdapter lotesAdapter;
	private ArrayAdapter<Company> companiesList;
	
	private AddLoteDialog addLoteDialog;
	private AddInvoiceDialog addInvoiceDialog;

	private LoteOriginEditDialog editLoteDialog;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.activity_origin);
		
		loggedUser = (User)getIntent().getSerializableExtra(LOGGED_USER_EXTRA);
		
		initializeCompanySpinner();
		initializeLotesListView();
		
		addLote = (Button)findViewById(R.id.add_lote_button);
		addLote.setOnClickListener(this);
		addLoteFromInvoice = (Button)findViewById(R.id.add_invoice_button);
		addLoteFromInvoice.setOnClickListener(this);
		destinationButton = (Button)findViewById(R.id.destination_button);
		destinationButton.setOnClickListener(this);
		
		setupActionBar();
	}
	
	private void initializeLotesListView() {
		// TODO Auto-generated method stub
		lotesListView = (ListView)findViewById(R.id.lotes_listView);
		lotesListView.setOnItemClickListener(this);
		lotesAdapter = new LoteOriginAdapter(this, R.layout.list_view_row_lote_origin, new ArrayList<LoteOrigin>());
		lotesListView.setAdapter(lotesAdapter);
		
	}

	private void initializeCompanySpinner() {
		companySpinner = (Spinner)findViewById(R.id.companySpinner);
		
		companiesList =  new ArrayAdapter<Company>(this, android.R.layout.simple_spinner_dropdown_item, new ArrayList<Company>());
		companySpinner.setAdapter(companiesList);
		
		companyWebService = new CompanyWebService(this, this);
		companyWebService.getCompaniesList();
	}

	/**
	 * Set up the {@link android.app.ActionBar}.
	 */
	private void setupActionBar() {

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
	    //getMenuInflater().inflate(R.menu.origin, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.add_lote_button:
			handleAddLoteButtonClick();
			break;
			
		case R.id.add_invoice_button:
			handleAddInvoiceButtonClick();
			break;
			
		case R.id.destination_button:
			handleDestinationButtonClick();
			break;
		
		default:
			break;
		}
		
	}
	
	private void handleDestinationButtonClick() {
		Intent intent = new Intent(getApplicationContext(),
                DestinationActivity.class);
		intent.putExtra(OriginActivity.LOGGED_USER_EXTRA, loggedUser);
        //startActivity(intent);
	}

	@Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Object selected = parent.getItemAtPosition(position);

        if(selected.getClass() == LoteOrigin.class) {
            LoteOrigin selectedLote = (LoteOrigin) selected;
            
            editLoteDialog = new LoteOriginEditDialog();
            editLoteDialog.setLote(selectedLote);
            editLoteDialog.show(getFragmentManager(), "Dialog");
        }
    }
	
	
	private void handleAddInvoiceButtonClick() {
		addInvoiceDialog = new AddInvoiceDialog();
		addInvoiceDialog.show(getFragmentManager(), "Dialog");
	}

	private void handleAddLoteButtonClick() {
		addLoteDialog = new AddLoteDialog();
		addLoteDialog.show(getFragmentManager(), "Dialog");		
	}

	@Override
	public void onGetCompaniesListError() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onGetCompaniesListResult(ArrayList<Company> companyArrayList) {
		companiesList.clear();
		companiesList.addAll(companyArrayList);	
		companiesList.notifyDataSetChanged();
		companyWebService.getDefaultCompany(loggedUser.getId());
	}

	@Override
	public void onGetDefaultCompanyError() {
		
	}

	@Override
	public void onGetDefaultCompanyResult(String companyId) {
		Company aux;
		for (int i = 0; i < companiesList.getCount(); i++){
            aux = companiesList.getItem(i);
            if(aux.getCodCompany().compareTo(companyId) == 0){
            	companySpinner.setSelection(i);                
            }
        }		
	}

	@Override
	public void onDialogAddLoteClick(String loteNumber) {
		LoteOrigin loteOrigin = new LoteOrigin(loteNumber, "1", "23", "Bacana", 5, "CX");
		lotesAdapter.add(loteOrigin);
		lotesAdapter.notifyDataSetChanged();		
	}

	@Override
	public void onDialogAddInvoiceClick(String invoiceNumber, String codFornec) {
		LoteOrigin loteOrigin = new LoteOrigin( "00000001", "1", "23", "Bacana vakdmv", 45, "CX");
		lotesAdapter.add(loteOrigin);
		loteOrigin = new LoteOrigin( "00000002", "1", "23", "Bacana vakdmv", 45, "CX");
		lotesAdapter.add(loteOrigin);
		loteOrigin = new LoteOrigin( "00000003", "1", "23", "Bacana vakdmv", 45, "CX");
		lotesAdapter.add(loteOrigin);
		loteOrigin = new LoteOrigin( "00000004", "1", "23", "Bacana vakdmv", 45, "CX");
		lotesAdapter.add(loteOrigin);
		loteOrigin = new LoteOrigin( "00000005", "1", "23", "Bacana vakdmv", 45, "CX");
		lotesAdapter.add(loteOrigin);
		loteOrigin = new LoteOrigin( "00000006", "1", "23", "Bacana vakdmv", 45, "CX");
		lotesAdapter.add(loteOrigin);
		loteOrigin = new LoteOrigin( "00000007", "1", "23", "Bacana vakdmv", 45, "CX");
		lotesAdapter.add(loteOrigin);	
		
		lotesAdapter.notifyDataSetChanged();		
	}

	@Override
	public void onDialogEditLoteSave(LoteOrigin lote) {
		lotesAdapter.notifyDataSetChanged();
	}

	@Override
	public void onDialogEditLoteRemove(LoteOrigin lote) {
		lotesAdapter.remove(lote);
		lotesAdapter.notifyDataSetChanged();		
	}
	
	
}
